﻿using UnityEditor;
using UnityEngine;

public class RemoveMissing
{
    [MenuItem("Tools/RemoveMissing")]
    public static void CheckReferenceMissing()
    {
        Transform[] selectionObjs = Selection.GetTransforms(SelectionMode.Deep);
        for (int i = 0; i < selectionObjs.Length; i++)
        {
            Debug.Log("<color=blue>" + selectionObjs[i].name + "</color>");
            var gameObject = selectionObjs[i].gameObject;
            var serializeObject = new SerializedObject(gameObject);
            var prop = serializeObject.FindProperty("m_Component");

            int r = 0;
            var components = gameObject.GetComponents<Component>();
            for (int j = 0; j < components.Length; j++)
            {
                if (components[j] == null)
                {
                    prop.DeleteArrayElementAtIndex(j - r);
                    r++;
                }
            }
            serializeObject.ApplyModifiedProperties();
        }
    }
}